<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Jpc extends CI_Controller {
	
	public function __construct() 
	{
		parent::__construct();
		
		$this->load->model('model_jpc');
		$this->load->model('model_auth');
		//$this->load->helper('form');
		//$this->load->helper('captcha');
		//$this->load->library('form_validation');
		//$this->load->database();
		$this->model_auth->_must_logged_in('jpc');
		
	}
	
	public function index () {

		$data = array(
			'userdata'=>$this->model_jpc->_get_userdata(),
			'warning' =>$this->model_jpc->_make_warning(),
		);
		
		$this->load->view('header', 
			array('title'=> 'User Menu ITB Junior Programming Contest 2011'));
		$this->load->view('jpc/user_menu',$data);
		$this->load->view('footer');
	}
	
	public function edit() {
	
		$data = array(
			'userdata'=>$this->model_jpc->_get_userdata($this->model_auth->_get_usertype_id()));
	
		$this->load->view('header', 
			array('title'=> 'Edit Data Pendaftaran ITB Junior Programming Contest 2011'));
		$this->load->view('jpc/user_edit',$data);
		$this->load->view('footer');
	}

	public function _valid () {
		$this->valid = $this->form_validation;
		//RULE - nama ruas - nama org - parameter 
		
		$this->valid->set_rules('jpc_nama','Nama anda','required|min_length[4]|max_length[50]');
		$this->valid->set_rules('jpc_nohp','Nomor ponsel anda','required|is_natural');
		$this->valid->set_rules('jpc_email','Alamat email anda','required|valid_email');
		$this->valid->set_rules('jpc_nama_sek','Nama sekolah','required|min_length[3]|max_length[100]');
		$this->valid->set_rules('jpc_alamat_sek','Alamat sekolah','required|min_length[3]|max_length[200]');
		$this->valid->set_rules('jpc_pembimbing','Nama pembimbing','required|min_length[3]|max_length[50]');
		
		if (	($this->input->post('old_password')!='')||
				($this->input->post('new_password')!='')||
				($this->input->post('new_password_2')!='')) {
				
			$this->valid->set_rules('old_password','Password lama','required|min_length[6]|max_length[20]');
			$this->valid->set_rules('new_password','Password baru','required|min_length[6]|max_length[20]|matches[new_password_2]');
			$this->valid->set_rules('new_password_2','konfirmasi password baru','required');
		}
		
		$this->valid->set_error_delimiters('<div class="error">','</div>');
		//$this->valid->set_message('required_chgpass',' harus diisi jika ingin melakukan penggantian password');
		$this->valid->set_message('matches','%s dan %s harus sama.');
		$this->valid->set_message('required','%s wajib diisi.');
		$this->valid->set_message('min_length','%s minimal  terdiri dari %s karakter.');
		$this->valid->set_message('max_length','%s maksimal terdiri dari %s karakter.');
		$this->valid->set_message('is_natural','%s hanya boleh terdiri dari angka saja.');
		$this->valid->set_message('valid_email','Email yang anda cantumkan tidak valid.');
		
		return ($this->valid->run()==FALSE)? FALSE :TRUE ;
		
	}
	
	
	public function validasi ($param='') {
	
		if ($this->input->post()==NULL) {
			redirect('jpc/','location');
			}
		
		if ($param=='sukses') {
			$this->load->view('redirect', array('message'=>'Pengubahan data pendaftaran sukses !'));
		} else if ($param=='suksespass') {
			$this->load->view('redirect', array('message'=>'Pengubahan data pendaftaran dan password sukses !'));
		} else {

			if ($this->_valid()==FALSE) { 
				//kembali ke form, tampilkan pesan error, apapun yang dimasukan tadi
				$this->edit();
			} else {
				$val='';
				if ($this->input->post('old_password')) {
					$val=$this->tank_auth->change_password($this->input->post('old_password'),$this->input->post('new_password'));
					if (!$val) {
						show_error('Password anda salah, penggantian password gagal');
						}
					}
				$data= $this->input->post();
				$this->model_jpc->_edit_kontestan($data);
				$this->validasi('sukses'.(($val!='')?'pass':''));
			}
		}
	}
	
	public function upload() {
		$data = array(
			'userdata'=>$this->model_jpc->_get_userdata($this->model_auth->_get_usertype_id()),
			);
		$this->load->view('header', 
			array('title'=> 'Unggah Syarat Pendaftaran ITB Junior Programming Contest 2011'));
		$this->load->view('jpc/upload',$data);
		$this->load->view('footer');
	}
	
	public function do_upload() {
		if ($this->input->post()==NULL) {
			redirect('jpc/','location');
			}
			
		$config['upload_path'] = 'berkas/jpc/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '400';
		$config['max_width']  = '800';
		$config['max_height']  = '800';	
		$config['encrypt_name']  = FALSE;
		$config['overwrite']  = TRUE;
		
		
		$no_reg= $this->model_auth->_get_usertype_id();
		$config['file_name']= md5('JPC'.$no_reg.'_ktm');
		$this->upload->initialize($config);
		$this->upload->do_upload('scan_kpelajar');
		$data=$this->upload->data();
		
		$error_1= $this->upload->display_errors('<div class="error">','</div>');
		$link_1 = ($error_1=='')?$config['upload_path'].$data['file_name']:'';
		
		
		$config['file_name']= md5('jpc'.$no_reg.'_buktibayar');
		$this->upload->initialize($config);
		$this->upload->do_upload('scan_buktibayar');
		$data=$this->upload->data();
		
		$error_4= $this->upload->display_errors('<div class="error">','</div>');
		$link_4 = ($error_4=='')?$config['upload_path'].$data['file_name']:'';
		
	
		//jpc_link_ktm1 	jpc_link_ktm2 	jpc_link_ktm3 	jpc_link_buktibayar
		$data = array( 
			//'upload_path'=>$config['upload_path'],
			'jpc_link_kpelajar'=>$link_1,
			'jpc_link_buktibayar'=>$link_4,
			);
		//print_r($data);
		//pass ke database
		$this->model_jpc->_insert_scan_data($data);
		
		$this->load->view('redirect', array('message'=>'Upload sukses !'));	
	}
	
	public function download() {
		
		
			$data = $this->model_jpc->_get_userdata($this->model_auth->_get_usertype_id());
		
		if ($data->jpc_valid) 
			{	
			$no_tim = $data->jpc_no_tim; 
			$id_tim = sprintf('JPC%03d',$no_tim);
			
			
			$query = $this->db->get_where('cert',array('username' => $id_tim));
			$row = $query->row();
			
			//print_r ($row);
			$cert_id = $row->cert_id;
			
			
			$nama_file = sprintf('jpc-%03d.jpg',$cert_id);
			
			//echo $nama_file;
			
			$data = file_get_contents("../../file-sertifikat/jpc/$nama_file"); // Read the file's contents
			$name = 'sertifikat.jpg';

			force_download($name, $data);
			
		}
	}
	
	public function kaos() {
	
		$data = array(
			'userdata'=>$this->model_jpc->_get_userdata($this->model_auth->_get_usertype_id()));
	
		$this->load->view('header', 
			array('title'=> 'Form Kaos Pendaftaran ITB Junior Programming Contest 2011'));
		$this->load->view('jpc/form_kaos',$data);
		$this->load->view('footer');
	}
		
	
	public function kaos_validasi ($param='') {
	
		if ($this->input->post()==NULL) {
			redirect('jpc/','location');
			}
		
		if ($param=='sukses') {
			$this->load->view('redirect', array('message'=>'Pengisian data kaos sukses !'));
		} else {
			$data=$this->input->post();
			$this->model_jpc->_insert_kaos_data($data);
			$this->kaos_validasi('sukses');
		}
	}
	
	// public function mail_test () {
		// $data=$this->model_jpc->_get_userdata();
		// $data->username='JPC001';
		// $data->password='AWSADRTT';
		// $this->model_jpc->_sendmail($data);
	// }
}

	